package com.ouyy.volunteerservice.volunteer.utils;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


/**
 * 发送邮件工具类
 */
public class EmailUtils {

    public static void sendEmail(String toEmail, String subject, String password) throws MessagingException {
        // 配置网易邮箱 SMTP 服务器
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.163.com"); // 网易 SMTP 服务器
        props.put("mail.smtp.auth", "true"); // 需要身份验证
        props.put("mail.smtp.starttls.enable", "true"); // 启用 TLS
        props.put("mail.smtp.port", "465"); // 端口号（465 使用 SSL）
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // SSL 连接

        // 认证信息（你的邮箱账号和授权码）
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("m19065300975@163.com", "WCedivZHnTxGu9BY"); // 替换为你的邮箱和授权码
            }
        });

        // 创建邮件消息
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("m19065300975@163.com")); // 发送方邮箱
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail)); // 接收方邮箱
        message.setSubject(subject); // 邮件标题

        // HTML 邮件内容
        String body = "<!DOCTYPE html>" +
                "<html lang='zh'>" +
                "<head>" +
                "<meta charset='UTF-8'>" +
                "<meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
                "<title>密码找回</title>" +
                "<style>" +
                "body { font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px; }" +
                ".container { max-width: 500px; background: white; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }" +
                ".header { font-size: 24px; font-weight: bold; color: #333; text-align: center; }" +
                ".content { font-size: 16px; color: #555; line-height: 1.6; margin-top: 10px; }" +
                ".password-box { font-size: 18px; font-weight: bold; color: #d9534f; background: #f8d7da; padding: 10px; border-radius: 5px; text-align: center; margin-top: 15px; }" +
                ".footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }" +
                "</style>" +
                "</head>" +
                "<body>" +
                "<div class='container'>" +
                "<div class='header'>密码找回</div>" +
                "<div class='content'>" +
                "您好，<br>您的密码是：" +
                "<div class='password-box'>" + password + "</div>" +
                "<br>请妥善保管您的密码，如果不是您本人操作，请尽快修改密码。" +
                "</div>" +
                "<div class='footer'>如果您没有请求此操作，请忽略此邮件。</div>" +
                "</div>" +
                "</body>" +
                "</html>";

        // 设置 HTML 内容
        message.setContent(body, "text/html;charset=UTF-8");

        // 发送邮件
        Transport.send(message);
    }

}
